package com.miukoo.aikao.qpaper.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.miukoo.aikao.core.dtos.*;
import com.miukoo.aikao.core.pojos.SecurityUser;
import com.miukoo.aikao.core.utils.ApiUtils;
import com.miukoo.aikao.core.utils.AppUserUtils;
import com.miukoo.aikao.core.utils.BeanMapperUtils;
import com.miukoo.aikao.qpaper.dto.PaperDto;
import com.miukoo.aikao.qpaper.dto.ext.PaperQuestionDetailDto;
import com.miukoo.aikao.qpaper.dto.request.PaperAnswerDto;
import com.miukoo.aikao.qpaper.dto.request.PaperCreateReqDto;
import com.miukoo.aikao.qpaper.dto.request.PaperListReqDto;
import com.miukoo.aikao.qpaper.dto.response.ExamDetailRespDto;
import com.miukoo.aikao.qpaper.dto.response.ExamResultRespDto;
import com.miukoo.aikao.qpaper.dto.response.PaperListRespDto;
import com.miukoo.aikao.qpaper.pojos.Paper;
import com.miukoo.aikao.qpaper.service.PaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* <p>
* 试卷控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-05-25 16:33
*/
@Api(tags={"试卷"})
@RestController
@RequestMapping("/exam/api/paper/paper")
public class PaperController {

    @Autowired
    private PaperService baseService;

    /**
    * 添加或修改
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "添加或修改")
    @RequestMapping(value = "/save", method = { RequestMethod.POST})
    public ApiResult<BaseIdRespDto> save(@RequestBody PaperDto reqDto) {
        //复制参数
        Paper entity = new Paper();
        BeanMapperUtils.copy(reqDto, entity);
        baseService.saveOrUpdate(entity);
        return ApiUtils.success(new BaseIdRespDto(entity.getId()));
    }

    /**
    * 批量删除
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/delete", method = { RequestMethod.POST})
    public ApiResult edit(@RequestBody BaseIdsReqDto reqDto) {
        //根据ID删除
        baseService.removeByIds(reqDto.getIds());
        return ApiUtils.success();
    }

    /**
    * 查找详情
    * @param reqDto
    * @return
    */
    @ApiOperation(value = "查找详情")
    @RequestMapping(value = "/detail", method = { RequestMethod.POST})
    public ApiResult<PaperDto> find(@RequestBody BaseIdReqDto reqDto) {
        Paper entity = baseService.getById(reqDto.getId());
        PaperDto dto = new PaperDto();
        BeanUtils.copyProperties(entity, dto);
        return ApiUtils.success(dto);
    }



    /**
     * 分页查找
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging", method = { RequestMethod.POST})
    public ApiResult<IPage<PaperListRespDto>> paging(@RequestBody PageReqDto<PaperListReqDto> reqDto) {
        SecurityUser user = AppUserUtils.getUser();
        reqDto.getParams().setUserId(user.getId());
        IPage<PaperListRespDto> page = baseService.paging(reqDto);
        return ApiUtils.success(page);
    }

    /**
     * 分页查找
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "分页查找")
    @RequestMapping(value = "/paging-admin", method = { RequestMethod.POST})
    public ApiResult<IPage<PaperListRespDto>> pagingAdmin(@RequestBody PageReqDto<PaperListReqDto> reqDto) {
        IPage<PaperListRespDto> page = baseService.paging(reqDto);
        return ApiUtils.success(page);
    }


    /**
     * 创建试卷
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "创建试卷")
    @RequestMapping(value = "/create-paper", method = { RequestMethod.POST})
    public ApiResult<BaseIdRespDto> save(@RequestBody PaperCreateReqDto reqDto) {
        SecurityUser user = AppUserUtils.getUser();
        Long paperId = baseService.createPaper(user.getId(), user.getUserName(), reqDto.getExamId());
        return ApiUtils.success(new BaseIdRespDto(paperId));
    }

    /**
     * 批量删除
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @RequestMapping(value = "/paper-detail", method = { RequestMethod.POST})
    public ApiResult<ExamDetailRespDto> paperDetail(@RequestBody BaseIdReqDto reqDto) {
        //根据ID删除
        ExamDetailRespDto respDto = baseService.paperDetail(reqDto.getId());
        return ApiUtils.success(respDto);
    }

    /**
     * 批量删除
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "试题详情")
    @RequestMapping(value = "/qu-detail", method = { RequestMethod.POST})
    public ApiResult<PaperQuestionDetailDto> quDetail(@RequestBody PaperQuestionDetailDto reqDto) {
        //根据ID删除
        PaperQuestionDetailDto respDto = baseService.findQuDetail(reqDto.getPaperId(), reqDto.getQuestionId());
        // 不直接返回答题的结果
        respDto.setIsRight(false);
        return ApiUtils.success(respDto);
    }

    /**
     * 填充答案
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "填充答案")
    @RequestMapping(value = "/fill-answer", method = { RequestMethod.POST})
    public ApiResult<PaperQuestionDetailDto> fillAnswer(@RequestBody PaperAnswerDto reqDto) {
        //根据ID删除
        baseService.fillAnswer(reqDto);
        return ApiUtils.success();
    }


    /**
     * 交卷操作
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "交卷操作")
    @RequestMapping(value = "/hand-exam", method = { RequestMethod.POST})
    public ApiResult<PaperQuestionDetailDto> handleExam(@RequestBody BaseIdReqDto reqDto) {
        SecurityUser user = AppUserUtils.getUser();
        baseService.handExam(reqDto.getId(),user.getId(),user.getUserName());
        return ApiUtils.success();
    }


    /**
     * 批量删除
     * @param reqDto
     * @return
     */
    @ApiOperation(value = "试卷详情")
    @RequestMapping(value = "/paper-result", method = { RequestMethod.POST})
    public ApiResult<ExamResultRespDto> paperResult(@RequestBody BaseIdReqDto reqDto) {
        //根据ID删除
        ExamResultRespDto respDto = baseService.paperResult(reqDto.getId(), false);
        return ApiUtils.success(respDto);
    }

}
